package com.xiaobawang.xjsyy_idea_plugin.forest;

import com.dtflys.forest.Forest;
import com.dtflys.forest.http.ForestRequest;
import com.dtflys.forest.http.ForestRequestType;
import com.xiaobawang.xjsyy_idea_plugin.global.GlobalConfig;

import java.nio.charset.StandardCharsets;

public class ForestApi {

    /**
     * @author synhard
     * @id 18101119
     * @description 运行研发阶段流水线
     * @date 2024/6/4 15:49
     * @param releaseWorkflowSn 流水线sn
     * @param releaseStageSn 流水线阶段sn
     * @param body qi请求体字符串
     * @return java.lang.String
     */
    public static String runPipeline(String releaseWorkflowSn, String releaseStageSn, String body) {
        // 构造链接
        String url = GlobalConfig.YUNXIAO_DOMAIN + GlobalConfig.RUN_PIPELINE.formatted(GlobalConfig.curOrgId,
                GlobalConfig.appName, releaseWorkflowSn, releaseStageSn);
        // 创建请求
        ForestRequest<?> request = Forest.request();
        // 设置请求头
        request.addHeader("x-yunxiao-token", GlobalConfig.token);
        // 设置请求连接
        request.url(url);
        // 设置请求体
        request.addBody(body);
        // 指定内容形式
        request.setContentType("application/json");
        // 设置请求方法
        request.setType(ForestRequestType.POST);
        // 设置编码格式
        request.setContentEncoding(StandardCharsets.UTF_8.toString());
        // 设置超时时间
        request.setConnectTimeout(10000);
        request.setReadTimeout(10000);
        request.setTimeout(10000);// 执行请求并返回结果
        return request.execute(String.class);
    }

    /**
     * @author synhard
     * @id 18101119
     * @description 查找应用下所有的研发流程
     * @date 2024/5/24 17:05
     * @return java.lang.String
     */
    public static String releaseWorkflows() {
        // 构造链接
        String url = GlobalConfig.YUNXIAO_DOMAIN + GlobalConfig.RELEASE_WORK_FLOWS.formatted(GlobalConfig.curOrgId, GlobalConfig.appName);
        // 创建请求
        ForestRequest<?> request = Forest.request();
        // 设置请求头
        request.addHeader("x-yunxiao-token", GlobalConfig.token);
        // 设置请求链接
        request.url(url);
        // 设置请求方法
        request.setType(ForestRequestType.GET);
        // 执行请求并返回结果
        return request.execute(String.class);
    }

    /**
     * @author synhard
     * @id 18101119
     * @description 获取流水线详情
     * @date 2024/5/24 17:07
     * @param pipelineId 流水线id
     * @return java.lang.String
     */
    public static String pipelineDetail(Long pipelineId) {
        // 构造链接
        String url = GlobalConfig.YUNXIAO_DOMAIN + GlobalConfig.PIPELINE_DETAIL.formatted(GlobalConfig.curOrgId, pipelineId);
        // 创建请求
        ForestRequest<?> request = Forest.request();
        // 设置请求头
        request.addHeader("x-yunxiao-token", GlobalConfig.token);
        // 设置请求链接
        request.url(url);
        // 设置请求方法
        request.setType(ForestRequestType.GET);
        // 执行请求并返回结果
        return request.execute(String.class);
    }

    /**
     * @description 获取流水线运行实例历史
     * @author 18101119
     * @date 2024/5/27 18:33
     * @email songxinhang2023@e-chinalife.com
     * @id 18101119
     * @phone 13001321080
     */
    public static String runningHistory(Long pipelineId, Integer pageNumber, Integer pageSize) {
        // 构造链接
        String url = GlobalConfig.YUNXIAO_DOMAIN + GlobalConfig.RUNNING_HISTORY.formatted(GlobalConfig.curOrgId, pipelineId);
        // 创建请求
        ForestRequest<?> request = Forest.request();
        // 设置请求参数
        request.addQuery("perPage", pageSize);
        request.addQuery("page", pageNumber);
        // 设置请求头
        request.addHeader("x-yunxiao-token", GlobalConfig.token);
        // 设置请求链接
        request.url(url);
        // 设置请求方法
        request.setType(ForestRequestType.GET);
        // 设置超时时间
        request.setConnectTimeout(10000);
        request.setReadTimeout(10000);
        request.setTimeout(10000);// 执行请求并返回结果
        // 执行请求并返回结果
        return request.execute(String.class);
    }

    /**
     * @author synhard
     * @id 18101119
     * @description 获取流水线运行实例详情
     * @date 2024/5/30 19:14
     * @param pipelineId 流水线 id
     * @param pipelineRunId 流水线运行 id
     * @return java.lang.String
     */
    public static String runningDetail(Long pipelineId, Long pipelineRunId) {
        // 构造链接
        String url = GlobalConfig.YUNXIAO_DOMAIN + GlobalConfig.RUNNING_DETAIL.formatted(GlobalConfig.curOrgId, pipelineId, pipelineRunId);
        // 创建请求
        ForestRequest<?> request = Forest.request();
        // 设置请求头
        request.addHeader("x-yunxiao-token", GlobalConfig.token);
        // 设置请求链接
        request.url(url);
        // 设置请求方法
        request.setType(ForestRequestType.GET);
        // 设置超时时间
        request.setConnectTimeout(10000);
        request.setReadTimeout(10000);
        request.setTimeout(10000);
        // 执行请求并返回结果
        return request.execute(String.class);
    }
}
